package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.WorkOrderDetailsModel;
import com.czy.owner.utils.DecimalUtil;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.glide.GlideUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hedepu on 2017-08-29
 */

public class OrderDetailsGoodsAdapter extends BaseRecyclerAdapter<WorkOrderDetailsModel.GoodsItemList, OrderDetailsGoodsAdapter.OrderDetailsGoodsHolder> {

    private Context ctx;
    private String money = "";

    public OrderDetailsGoodsAdapter(Context context, List<WorkOrderDetailsModel.GoodsItemList> data) {
        super(context, data);
        this.ctx = context;
    }


    public void addALL(List<WorkOrderDetailsModel.GoodsItemList> list) {
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }

    public String getAllMoney() {
        if (StringUtils.isEmpty(money)) {
            for (WorkOrderDetailsModel.GoodsItemList goodsItemList : data) {
                if (StringUtils.isEmpty(goodsItemList.getUserPackageId()) || "0".equals(goodsItemList.getUserPackageId())) {
                    money = DecimalUtil.add(money, DecimalUtil.multiply(goodsItemList.getPrice(), goodsItemList.getCount() + ""));
                } else {
                }
            }
        }
        money = DecimalUtil.formatNum(money);
        return money;

    }

    @Override
    public void onBindViewHolder(OrderDetailsGoodsHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));

    }

    public List<WorkOrderDetailsModel.GoodsItemList> getList() {
        return data;
    }

    @Override
    public OrderDetailsGoodsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrderDetailsGoodsHolder(layoutInflater.inflate(R.layout.item_order_details_list, parent, false));
    }

    public class OrderDetailsGoodsHolder extends BaseRecyclerAdapter.ViewHolder {

        @BindView(R.id.img_logo)
        ImageView imgLogo;
        @BindView(R.id.tv_order_goods)
        TextView tvOrderGoods;
        @BindView(R.id.tv_order_goods_money)
        TextView tvOrderGoodsMoney;
        @BindView(R.id.tv_order_goods_original_price)
        TextView tvOrderGoodsOriginalPrice;
        @BindView(R.id.tv_order_goods_present_price)
        TextView tvOrderGoodsPresentPrice;
        @BindView(R.id.tv_order_goods_num)
        TextView tvOrderGoodsNum;
        @BindView(R.id.tv_order_goods_type)
        TextView tvOrderGoodsType;

        public OrderDetailsGoodsHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);


        }

        public void bindData(WorkOrderDetailsModel.GoodsItemList model) {
            tvOrderGoods.setText(model.getItemName());
            //原价工单详情的那个原价，如果是套餐项目的话就没有原价，非套餐项目有，但是自定义项目也没有，你注意一下。
            if (StringUtils.isEmpty(model.getUserPackageId()) || "0".equals(model.getUserPackageId())) {
//                tvOrderGoodsOriginalPrice.setVisibility(View.VISIBLE);
//                tvOrderGoodsOriginalPrice.setText(model.getOriginalPrice());
//                tvOrderGoodsOriginalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中间横线
//                tvOrderGoodsPresentPrice.setText(model.getPrice());
//                tvOrderGoodsMoney.setText(DecimalUtil.divide(model.getPrice(), model.getCount()));
                tvOrderGoodsMoney.setText("¥" + model.getPrice());
            } else {
                tvOrderGoodsMoney.setText("¥0.00");
//                tvOrderGoodsOriginalPrice.setVisibility(View.GONE);
//                tvOrderGoodsPresentPrice.setText(model.getPrice());
//                tvOrderGoodsMoney.setText(DecimalUtil.divide("0", model.getCount()));

            }
//            if ("goods".equals())
            tvOrderGoodsNum.setText("x" + model.getCount());
            if ("goods".equals(model.getItemType())) {
                if (tvOrderGoodsType.getVisibility() == View.GONE)
                    tvOrderGoodsType.setVisibility(View.VISIBLE);
                tvOrderGoodsType.setText(model.getSpecName());
            } else {
                if (tvOrderGoodsType.getVisibility() == View.VISIBLE)
                    tvOrderGoodsType.setVisibility(View.GONE);
            }

            //添加数据
            GlideUtils.loadImage(mContext, model.getLogo(), imgLogo, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_icon, R.mipmap.default_icon);
        }
    }

}

